//InnerAudioContext实例
const app = getApp()

 
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    pic_num:521,
    go_num : '2,1 A',
    left_num : '2,0 D',
    right_num :'2,0 B',
    id:'',
    animationMain:null,
    fshow:1,
    playRight:false,
    isok:0,
    answer:'',
    con:'study',
    focusInput: false,
    height: 0,
    isInput: false,
    showTip:0,
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
 
  //播放按钮事件
  playLeft : function(){
    wx.showLoading({
      title: "转身中...",
      mask: true
});
    let lnum = this.data.left_num;
    var id = this.data.id;
    this.timeUpdate(lnum);
  },
 
  //暂停按钮事件
  playForward : function(){
    wx.showLoading({
      title: "正在开门...",
      mask: true
});
    let show =  this.data.fshow;
    this.setData({
      fshow: 0,
    });

    let gnum = this.data.go_num;
    var id = this.data.id;
    this.timeUpdate(gnum);


    let delay = 600;
    let animation = wx.createAnimation({
      duration: 600,
      timingFunction: 'ease',
      //delay: delay
     });
     const fadein = animation.opacity(0).step().opacity(1).step().export();
     //const fadeout = animation.opacity(0).step().export();
     this.setData({
      //animationBack: show ? fadeout : fadein,
      animationMain: fadein,
      fshow: 0
    });
  },
 
  //停止按钮事件
  playRight : function(){

    wx.showLoading({
      title: "转身中...",
      mask: true
});
    let rnum = this.data.right_num;
    //var id = this.data.id;
    //this.doorChange(rnum);
    this.timeUpdate(rnum);
    
    
  },
 
  //进度条改变后触发事件
  doorChange : function(go_num){
    var that = this;
    //var id = this.data.id;
    //var gonum = go_num.replace(' ','').replace(',','');
    //var ret = wx.getStorageSync("doordata"+gonum) || null;
    //console.log('get data===id='+id+' go_num='+gonum+JSON.stringify(ret))
    this.timeUpdate(go_num);
    
    //this.playMusic();
    
  },
  //进度条拖动过程中触发事件
  playReset : function(e){
    
    this.setData({
      pic_num:521,
      go_num : '2,1 A',
      left_num : '2,0 D',
      right_num :'2,0 B',
      id:'',
    });
    //this.timeUpdate('2,1 A','521');
    //wx.removeStorageSync("doordata"+pic_num);

  },
 

 
  //播放的时候，更新进度条和时间显示
  timeUpdate : function(go_num){
    var that = this;
    
    wx.request({
      url: app.globalData.publicAdress + '/api/game/doorinfo',
      method: "post",
      header: {
        'content-type': 'application/json', // 默认值
        'Accept': 'application/vnd.cowsms.v2+json',
        'Authorization': 'Bearer ' + wx.getStorageSync("token"),
      },
      data: {
        name: go_num,
      },
      success: function (res) {
        //wx.hideLoading()
        //console.log(JSON.stringify(res))
        //如果token失效   则返回新的token  
        if (res.header.Authorization) {
          var str = res.header.Authorization;
          // console.log(str)
          wx.removeStorageSync("token");
          wx.setStorageSync("token", str.substring(7, str.length))
        }
        if (res.statusCode == '200') {
          if(res.data.data != null){
            var ret = res.data.data;
            console.log('ret==='+JSON.stringify(ret));
                        
            that.setData({
              go_num: ret.forward_door,
              left_num: ret.left_door,
              right_num: ret.right_door,
              pic_num:ret.num,
              id:ret.id,
              con:'study',
            })

            //console.log('new datalist==='+JSON.stringify(datalist));
          }else{
            wx.showModal({
              title: '提示',
              content: '门锁了？',
              success: (res) => {
                  // wx.switchTab({
                  //   url: '/pages/strategyList/strategyList'
                  // })                
              }
            })
          }

          setTimeout(function () {
            wx.hideLoading({});
          }.bind(this), 600);

        }
      }
    })


    
  },
  onBack(){
    wx.navigateBack({
    })
  },
  onOk(){
    var that = this;
    console.log('键盘确认')
    console.log('输入内容==='+that.data.answer+' 答案==='+that.data.con )
    
    if(that.data.answer.length == 0){
      wx.setStorageSync("hpzIsOk", 1);
      wx.showToast({
        title: '请输入答案',
        icon: "error",
        duration: 2000
      })
    }else{
      if(that.data.con != ''){
        if(that.data.answer == that.data.con){
          // wx.showToast({
          //   title: '答案正确',
          //   icon: "success",
          //   duration: 2000
          // })
          this.setData({
            showTip:2,
          })
          
          
        }
        else
        {
          // wx.showToast({
          //   title: '答案错误',
          //   icon: "error",
          //   duration: 2000
          // })
          this.setData({
            showTip:1,
          })
          wx.setStorageSync("hpzIsOk", 1);
        }
        
      }
      else{
        
      }
      

    }

    // wx.reLaunch({
    //   url: '/pagesD/pages/gameinfo/playSelect?id=' + that.data.game_id //传递得游戏id,
    // })
  },
  //关闭提示
  onCancelTip() {
    console.log('点我了onCancelTip')
    this.setData({
      showTip: 0,
    })
  },
  onOKTip(){
    var that = this;
    var curTaskID = wx.getStorageSync("curTaskID");
              if(curTaskID){
                let idss = curTaskID.split('-');
                let mainid = idss[0];
                if(mainid == that.data.type){
                  let taskid = idss[1];
                  // if(line_id < taskid){
                  //   line_id = parseInt(taskid);
                  // }
                }
                wx.setStorageSync("hpzIsOk", 2);
                console.log('next curTaskID='+curTaskID+' gameid='+that.data.game_id +' type='+mainid+' isOk=2');
                wx.redirectTo({
                  url: '../../gameinfo/playButton?id=19&type='+mainid+'&next=0&isok=2' //传递得游戏id,
                })
              }
  },
  //用户名和密码输入框事件
  userNameInput2:function(e){
    console.log('输入框事件==='+e.detail.value)
    this.setData({
      answer:e.detail.value
    })
  },
  inputFocus(e) {
    console.log(e, '键盘弹起')
    this.setData({
      height: e.detail.height,
      isInput: true
    })
  },
  inputBlur() {
    console.log('键盘收起')
    this.setData({
      isInput: false,
      height:0
    })
  },
  focusButn: function () {
    var that = this;
    console.log('键盘显示',that.data.con)

    that.setData({
      focusInput: true,
      isInput: true
    })

  },
  
  leifent_rotate_click_left: function(per,isload) {
    var that = this
       
      //var rotate=that.data.leifengta_rotate+10.8;
      var rotate=that.data.leifengta_rotate+10.8;
      var rotate2 = that.data.changzhen_rotate+0.2;
      if(isload==1){
        rotate = 10.8*per;
        rotate2 = 0.2*per+45;
      }
 
      
         console.log('rotate==='+rotate);

         that.setData({
           leifengta_rotate:rotate,
           changzhen_rotate:rotate2,
         })
         //console.log('旋转1  top='+top + ' left='+left);
       //this.data.leifengta_rotate++;
       //that.rotateAni(++this.data.leifengta_rotate);
    },
      /**
 * 实现image旋转动画，每次旋转 45*n度
 */

})